package msu.erdc.hci;

import java.util.Arrays;
import java.util.List;

import msu.erdc.hci.customObjects.AccidentObject;
import msu.erdc.hci.customObjects.InsuranceObject;
import msu.erdc.hci.customObjects.PassengerObject;
import msu.erdc.hci.customObjects.VehicleObject;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class VehicleOccupant extends Fragment {

	PassengerObject passenger;
	int accidentIndex;
	int vehicleIndex;
	int occupantIndex;
	TextView breadCrumb;
	EditText firstName;
	EditText lastName;
	EditText address;
	EditText city;
	EditText state;
	EditText postal;
	EditText phone;
	CheckBox isDriver;
	CheckBox isInjured;
	
	Button insuranceInfoButton;
	Button injuryDescButton;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view =  inflater.inflate(R.layout.vehicle_occupant_form, container, false);
		//do ui stuff here
		final FragmentManager manager = getFragmentManager();
		
		breadCrumb = (TextView) view.findViewById(R.id.breadVOTextView);
		breadCrumb.setText(((Global)getActivity().getApplication()).accidents.get(accidentIndex).toString() + "/" + 
		((Global)getActivity().getApplication()).accidents.get(accidentIndex).reportItemList.vehicleList.get(vehicleIndex).year + " " +
		((Global)getActivity().getApplication()).accidents.get(accidentIndex).reportItemList.vehicleList.get(vehicleIndex).make + " " +
		((Global)getActivity().getApplication()).accidents.get(accidentIndex).reportItemList.vehicleList.get(vehicleIndex).model + "/Occupant:");
		 
		
		firstName = (EditText) view.findViewById(R.id.firstNameVOEditText);
		firstName.setText(passenger.firstName);
		
		lastName = (EditText) view.findViewById(R.id.lastNameVOEditText);
		lastName.setText(passenger.lastName);
		
		address = (EditText) view.findViewById(R.id.addressVOEditText);
		address.setText(passenger.address);
		
		city = (EditText) view.findViewById(R.id.cityVOEditText);
		city.setText(passenger.city);
		
		state = (EditText) view.findViewById(R.id.stateVOEditText);
		state.setText(passenger.state);
		
		postal = (EditText) view.findViewById(R.id.postalVOEditText);
		postal.setText(passenger.postal);
		
		phone = (EditText) view.findViewById(R.id.phoneVOEditText);
		phone.setText(passenger.phone);
		
		isDriver = (CheckBox) view.findViewById(R.id.driverVOCheckBox);
		isDriver.setChecked(passenger.isDriver);
		isDriver.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
					insuranceInfoButton.setVisibility(Button.VISIBLE);
				else
					insuranceInfoButton.setVisibility(Button.INVISIBLE);
			}
		});
		
		isInjured = (CheckBox) view.findViewById(R.id.injuredVOCheckBox);
		isInjured.setChecked(passenger.isInjured);
		isInjured.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
					injuryDescButton.setVisibility(Button.VISIBLE);
				else
					injuryDescButton.setVisibility(Button.INVISIBLE);
			}
		});
		
		insuranceInfoButton = (Button) view.findViewById(R.id.button1);
		injuryDescButton = (Button) view.findViewById(R.id.button2);
		
		insuranceInfoButton.setOnClickListener(unsupportedClick);
		injuryDescButton.setOnClickListener(unsupportedClick);
		
		if(!passenger.isDriver)
			insuranceInfoButton.setVisibility(Button.INVISIBLE);
		if(!passenger.isInjured)
			injuryDescButton.setVisibility(Button.INVISIBLE);
		
		return view;
	}
	
	OnClickListener unsupportedClick = new OnClickListener(){

		@Override
		public void onClick(View v) {
			showUnsupportedToast();
		}};
	
	private void showUnsupportedToast()
	{
		Toast.makeText(getActivity(), "Interactivity is limited in this prototype.", Toast.LENGTH_SHORT).show();
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO
        //if not passed in
        Bundle arguments = getArguments();
        if(!arguments.containsKey("occupantIndex"))
        	passenger = new PassengerObject();
        else
        	passenger = ((Global)getActivity().getApplication()).accidents.get(arguments.getInt("accidentIndex")).reportItemList.vehicleList.get(arguments.getInt("vehicleIndex")).passengerList.get(arguments.getInt("occupantIndex"));
		
        accidentIndex = arguments.getInt("accidentIndex");
        vehicleIndex = arguments.getInt("vehicleIndex");
        occupantIndex = arguments.getInt("occupantIndex");
        
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		
	}
	
}